package org.zj.webview.type;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * WebView2环境设置
 *
 * @author zj
 * @since 2025/6/21
 */
@Accessors(chain = true)
@Data
public class WebViewSettings {
    private String workspace = "./";
    private WebViewData webview;
    private boolean implicitExit;

    @Override
    public String toString() {
        StringBuilder json = new StringBuilder();
        json.append("{");
        if (workspace != null) {
            json.append("\"workspace\":\"").append(escapeString(workspace)).append("\",");
        }
        if (webview != null) {
            json.append("\"webview\":").append(webview).append(",");
        }
        json.append("\"implicitExit\":").append(implicitExit).append(",");
        // 删除末尾多余的逗号并闭合括号
        if (json.charAt(json.length() - 1) == ',') {
            json.deleteCharAt(json.length() - 1);
        }
        json.append('}');
        return json.toString();
    }


    @Data
    public static class HostMappingData {
        private boolean enable;
        private String type = "";
        private String host = "";
        private String path = "";
        private String cors = "";

        @Override
        public String toString() {
            StringBuilder json = new StringBuilder();
            json.append("{");
            json.append("\"enable\":").append(enable).append(",");
            if (type != null) {
                json.append("\"type\":\"").append(escapeString(type)).append("\",");
            }
            if (host != null) {
                json.append("\"host\":\"").append(escapeString(host)).append("\",");
            }
            if (path != null) {
                json.append("\"path\":\"").append(escapeString(path)).append("\",");
            }
            if (cors != null) {
                json.append("\"cors\":\"").append(escapeString(cors)).append("\"");
            }
            // 删除末尾多余逗号并闭合括号
            if (json.charAt(json.length() - 1) == ',') {
                json.deleteCharAt(json.length() - 1);
            }
            json.append('}');
            return json.toString();
        }

    }

    @Data
    public static class WebViewData {
        private String additionalArguments = "";
        private HostMappingData hostMapping;
        private List<String> ignoreSslUrls;

        @Override
        public String toString() {
            StringBuilder json = new StringBuilder();
            json.append("{");
            if (additionalArguments != null) {
                json.append("\"additionalArguments\":\"").append(escapeString(additionalArguments)).append("\",");
            }
            if (hostMapping != null) {
                json.append("\"hostMapping\":").append(hostMapping).append(",");
            }
            if (ignoreSslUrls != null) {
                json.append("\"ignoreSslUrls\":[");
                for (String ignoreSslUrl : ignoreSslUrls) {
                    json.append("\"").append(escapeString(ignoreSslUrl)).append("\",");
                }
                if (json.charAt(json.length() - 1) == ',') {
                    json.deleteCharAt(json.length() - 1);
                }
                json.append(']').append(",");
            }
            if (json.charAt(json.length() - 1) == ',') {
                json.deleteCharAt(json.length() - 1);
            }
            json.append('}');
            return json.toString();
        }
    }

    /**
     * 转义字符串
     *
     * @param str
     * @return
     */
    private static String escapeString(String str) {
        if (str == null) {
            return null;
        }
        int len = str.length();
        char[] out = new char[len * 2]; // 最坏情况需要双倍长度
        int idx = 0;
        for (int i = 0; i < len; i++) {
            char c = str.charAt(i);
            switch (c) {
                case '\\':
                    out[idx++] = '\\';
                    out[idx++] = '\\';
                    break;
                case '"':
                    out[idx++] = '\\';
                    out[idx++] = '"';
                    break;
                case '\n':
                    out[idx++] = '\\';
                    out[idx++] = 'n';
                    break;
                case '\r':
                    out[idx++] = '\\';
                    out[idx++] = 'r';
                    break;
                case '\t':
                    out[idx++] = '\\';
                    out[idx++] = 't';
                    break;
                case '\b':
                    out[idx++] = '\\';
                    out[idx++] = 'b';
                    break;
                case '\f':
                    out[idx++] = '\\';
                    out[idx++] = 'f';
                    break;
                default:
                    out[idx++] = c;
                    break;
            }
        }
        return new String(out, 0, idx);
    }
}
